define(
		[ "PDUtilDir/util", "PDUtilDir/grid", "PDUtilDir/slidebar", "PDUtilDir/inputSelect" ],
		function(Util, Grid, Slidebar, inputSelect) {
			function AssignSupportInit($scope, $compile) {
				return new AssignSupport($scope, $compile);
			}
			function AssignSupport($scope, $compile) {
				this.$scope = $scope;
				this.$compile = $compile;
			}
			AssignSupport.fn = AssignSupport.prototype;
			AssignSupport.fn.init = function(id) {
                var _this = this,
                    allData = this.$scope.grid.getAllData();

                this.entity = {
                    groupId : id,
                    guideId: undefined
                };

                $.each(allData, function(index, row) {
                    if(row.id == id){
                        _this.entity = $.extend(_this.entity, row);
                        return;
                    }
                });

				// console.info(_this.entity);

				this.cache = {
					status : new Array(),
					statusMap: new Map()
				};
				this.saerch = {};
			}
			AssignSupport.fn.digest = function() {
				this.$scope.$digest();
			}
			AssignSupport.fn.run = function(id) {
				var _this = this;
				_this.init(id);
				_this.slidebars = Slidebar({
					size : 'lg',
					url : getServer() + "/static/app/group/groupInfos/views/assign.html",
//					close : false,
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function() {
						_this.initialization();
						_this.digest();
					}
				});
			};
			AssignSupport.fn.initialization = function() {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/getGuideAuditStatus.htmls",
					success : function(data) {
						_this.cache.status = data.data;
						$.each(data.data, function(index, row) {
							_this.cache.statusMap.set(row.dictCode, row.dictName);
						});
						_this.initGrid();
					}
				});
                //加载code
                $.ajax({
                    url : getServer() + "/flea/hclv/getServiceOrgTypeTwo.htmls",
                    success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
                        //车辆类型
                        inputSelect.dropCache();
                        inputSelect({
                            id : "serviceOrgType",
                            key : {
                                id : "dictCode",
                                name : "dictName",
                                data : "data",
                            },
                            data : data.data,
                            simpleData : true,
                            searchAble : false,
                            callback : function(ids) {
                            	var type = ids[0];
                            	//供应商
                            	if(type == "D_SERVICE_ORG_SUPPLIER"){
                                    _this.entity.depId = _this.entity.supplierId;
								}else if(type == "D_SERVICE_ORG_GROUP")
                                    _this.entity.depId = _this.entity.serviceOrgId;
                                _this.$scope.assignPayForm.depId.$setViewValue(_this.entity.depId);
                            }
                        });
                    }
                });
//				$.ajax({
//					url : getServer() + "/flea/hclv/getCertificates.htmls",
//					success : function(data) {
//						_this.cache.certificates = data.data;
//						var certificates = data.data, len = certificates.length, obj = new Map();
//						for(var i = 0; i < len; i++){
//							_this.cache.certificatesMap.set(certificates[i].dictCode, certificates[i].dictName);
//						}
//						_this.initGrid();
//					}
//				});
			};
			AssignSupport.fn.initGrid = function() {
				var _this = this;
				_this.grid = Grid({
					id : "assignGuideForGrid",
					index : true,
					multi : false,
					pagination : false,
					cache : false,
					onSeletedChanged : function(data, data1) {
						if(data.length == 0){
							_this.entity.guideId = undefined;
							_this.$scope.assignGuideForm.isOk.$setViewValue(undefined);
							return;
						}
						_this.$scope.assignGuideForm.isOk.$setViewValue(true);
						_this.entity.guideId = data[0].id;
					},
					layout : [ {
						name : "姓名",
						field : "guideName",
						sort : true
					}, {
						name : "联系电话",
						field : "guidePhone",
						sort : true
					}, {
						name : "身份证",
						field : "certificatesNumber"
					}, {
						name : "状态",
						field : "auditStatus",
						format : function(e) {
							return _this.cache.statusMap.get(e.row.auditStatus);
						}
					} ],
					toolbar : [ {
						name : "查询",
						icon : "fa fa-trash-o",
						callback : function(event) {
							_this.searchFun();
						}
					} ],
					data : {
						type : 'URL',
						value : getServer() + '/flea/hclv/guide/selectAll.htmls'
					}
				});
			};
			AssignSupport.fn.searchFun = function() {
				this.grid.reload({
					queryParam : this.search
				})
			};
			AssignSupport.fn.save = function() {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/guide/assignGuide.htmls",
					data : {
						groupId : _this.entity.groupId,
						guideId: _this.entity.guideId,
                        guideCost: _this.entity.guideCost,
                        deptId: _this.entity.depId
					},
					success : function(data) {
						if(data.status != 200){
							return
						}
                        Util.alert(data.message);
						_this.slidebars.close();
						_this.$scope.grid.reload();
					}
				});
			};
			return AssignSupportInit;
		});